local Buff 	  = require("app.battleV3.ui.objs.Buff")
local BarStatus = require("app.battleV3.ui.objs.BarStatus")
local Head 	  = require("app.battleV3.ui.objs.Head")
local TargetLeft = MClass("TargetLeft")
local Utils = require("app.utils.Util")

local TeamName_Color = cc.c4b(189,255,0, 255)
local EnemyName_Color = cc.c4b(255,1,1, 255)

--ctor 初始化UI init设置静态数据 setData设置动态数据
function TargetLeft:ctor()
    --self.root 默认为根节点
    --Leader
    local leaderSide = {}
    self.leaderPanel = self:findChild("Panel/Panel_leader")
    leaderSide.name = self.leaderPanel:findChild("Text_2")
    leaderSide.head = Head.new(self.leaderPanel:findChild("Panel_head"))
    leaderSide.buffView = self.leaderPanel:findChild("Panel_buff")
    leaderSide.bar  = BarStatus.new(self.leaderPanel)
    leaderSide.lockBtn = self.leaderPanel:findChild("Button_lock")
	leaderSide.lockBtn:setBright(true)
    leaderSide.signBtn = self.leaderPanel:findChild("Button_sign")
	leaderSide.signBtn:setBright(true)

    leaderSide._buffkeys = {}

    for i=1, BattleUIConst.BuffMax do
    	local key = "buff_"..i
		table.insert(leaderSide._buffkeys,key)
		leaderSide[key] = Buff.new(leaderSide.buffView:findChild(string.format("buff_%d",i)))
	end
    self.leaderSide = leaderSide

    --Team
    local teamSide = {}

    self.teamPanel = self:findChild("Panel/Panel_team")
    teamSide.name = self.teamPanel:findChild("Text_2")
    teamSide.head = Head.new(self.teamPanel:findChild("Panel_head"))
    teamSide.buffView = self.teamPanel:findChild("Panel_buff")
    teamSide.bar  = BarStatus.new(self.teamPanel)
    teamSide.lockBtn = self.teamPanel:findChild("Button_lock")
	teamSide.lockBtn:setBright(true)

    teamSide._buffkeys = {}

    for i=1, BattleUIConst.BuffMax do
    	local key = "buff_"..i
		table.insert(teamSide._buffkeys,key)
		teamSide[key] = Buff.new(teamSide.buffView:findChild(string.format("buff_%d",i)))
	end
    self.teamSide = teamSide

    --Cache
	self._cachedNameTable = {"name","  lv", 0}
	self._cachedNameStr = ""

	self.leaderPanel:setVisible(false)
	self.nowSide = self.teamSide
	self:init()
end

function TargetLeft:setAsLeader(bLeader,bEnemy)
	if bLeader and bEnemy then
		self.leaderPanel:setVisible(true)
		self.teamPanel:setVisible(false)
		self.nowSide = self.leaderSide
	else
		self.leaderPanel:setVisible(false)
		self.teamPanel:setVisible(true)
		self.nowSide = self.teamSide
	end
end

function TargetLeft:init()
   	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BUFF_UPDATE,handler(self,TargetLeft.updateBuffs))

   	local function clickCallBack(sender,eventType)
		if eventType == ccui.TouchEventType.began then
		elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
			BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_CANCEL_AUTO)
			if self.bLock then
				--已经锁了
				BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.TARGT_LOCK,false)
				self:setLock(false)
			else
				BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.TARGT_LOCK,true)
				self:setLock(true)
			end
		end
	end
	self.teamSide.lockBtn:addTouchEventListener(clickCallBack)
	self.leaderSide.lockBtn:addTouchEventListener(clickCallBack)

	local function clickMarkCallBack(sender,eventType)
		if eventType == ccui.TouchEventType.began then
		elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
			--已经锁了
			BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_CANCEL_AUTO)
			if self.bMarked then
				BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.TARGT_TEAM_MARK, {targetId = self.targetId, flagId = 0})
				self:setMark(false)
			else
				BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.TARGT_TEAM_MARK, {targetId = self.targetId, flagId = 1})
				self:setMark(true)
			end
		end
	end
	self.leaderSide.signBtn:addTouchEventListener(clickMarkCallBack)
end

function TargetLeft:updateBuffs(data,targetId)
	if targetId~=self.targetId then return end
	self:reset()
	local keys = self.nowSide._buffkeys
	local i = 1
	-- for i=1,#data do
	for k,v in pairs(data) do
		if keys[i] and cc.isAlive(self.nowSide[keys[i]]) then
			self.nowSide[keys[i]]:setTargetId(self.targetId)
			self.nowSide[keys[i]]:setData(v)
		end
		i = i + 1
	end
end

function TargetLeft:reset()
	local keys = self.nowSide._buffkeys
	for i=1, BattleUIConst.BuffMax do
		if keys[i] and cc.isAlive(self.nowSide[keys[i]]) then
			self.nowSide[keys[i]]:setTargetId(nil)
			self.nowSide[keys[i]]:setVisible(false)
		end
	end
end


function TargetLeft:setLock(bLock)
	self.bLock = bLock
	self.nowSide.lockBtn:setBright(not bLock)
end

function TargetLeft:setMark(bMarked)
	self.bMarked = bMarked
	if self.nowSide.signBtn then
		self.nowSide.signBtn:setBright(not bMarked)
	end
end

function TargetLeft:setEnemy(actorType)
	if actorType == Enums.BattleActorType.self or actorType == Enums.BattleActorType.teammate then
		self.nowSide.name:setTextColor(TeamName_Color)
	else
		self.nowSide.name:setTextColor(EnemyName_Color)
	end
end

function TargetLeft:setData(entityModel,view,targetId,bLeader)
	self:setVisible(true)
	self:setAsLeader(bLeader,view:isEnemy())
	--self.lockBtn:onUnSelected()
	self:setLock(false)
	self.nowSide.lockBtn:setTouchEnabled(true)
	-- self.lockBtn:setBright(false)
	self.targetId = targetId
	self:reset()
	self:setMark(view:getMarkId() == 1)

	local entityName = entityModel:getName()
	local needRebuild = false
	if self._cachedNameTable[1]~= entityName then
		self._cachedNameTable[1] = Utils.getStringInLen(entityName,Constant.BATTLE_SHOW_NAME_MAX_LEN)
		needRebuild = true

	end
	if self._cachedNameTable[3] ~= entityModel:getLv() then
		self._cachedNameTable[3] = entityModel:getLv()
		needRebuild = true
	end

	if needRebuild then
		self._cachedNameStr = table.concat(self._cachedNameTable, "")
	end

	self.nowSide.name:setString(self._cachedNameStr)
	local buffList = view:getBuffIconDict()
	self.nowSide.head:setData(view, entityModel)
	self.nowSide.bar:setData(view, entityModel)
	self:setEnemy(view:getActorType())
	local i = 1
	-- for i=1,#buffList do
	local keys = self.nowSide._buffkeys
	for k,v in pairs(buffList) do
		if i>BattleUIConst.BuffMax then
			--todo
			break
		end

		self.nowSide[keys[i]]:setTargetId(self.targetId)
		self.nowSide[keys[i]]:setData(v)
		i = i + 1
	end
end

function TargetLeft:clearData()
	self:setLock(false)
	self.leaderSide.bar:clearHideEffect()
	self.teamSide.bar:clearHideEffect()
end

function TargetLeft:updateData(targetId,hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax,hpExtra,hpMaxExtra)
	if targetId ~= self.targetId then return end
	if hpMin <= 0 then
		self:setVisible(false)
	end
	self.nowSide.bar:updateData(hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax,hpExtra,hpMaxExtra)
end

function TargetLeft:shouldHideAllHpUI(flag)
	self.nowSide.bar:shouldHideAllHpUI(flag)
end

return TargetLeft
